package aupdg.presentation;

import aupdg.logic.analysis.SecurityLevel;
import aupdg.data.model.Node;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

/**
 * Popup menu that appears when one right-clicks on a node.
 *
 * @author AA and CJ
 */
public class NodePopupMenu extends JPopupMenu {

    /**
     * Default constructor.
     */
    public NodePopupMenu() {
        super("Node Menu");
        ButtonGroup btnGroup = new ButtonGroup();
        JRadioButtonMenuItem highItem = new HighLevNodeRadioButton();
        JRadioButtonMenuItem lowItem = new LowLevNodeRadioButton();

        btnGroup.add(highItem);
        btnGroup.add(lowItem);
        this.add(highItem);
        this.add(lowItem);
    }

    /**
     * Implements an entry on the menu containing a node label (with low
     * security level) that after clicked sets it to high security level.
     */
    public class HighLevNodeRadioButton extends JRadioButtonMenuItem
            implements NodeMenuListener<Node> {

        Node n;
        VisualizationViewer vv;

        /**
         * Default constructor.
         */
        public HighLevNodeRadioButton() {
            super("High Security Level");
            this.addActionListener(new ActionListener() {

                /**
                 * Changes the security level to high.
                 */
                @Override
                public void actionPerformed(ActionEvent e) {
                    n.setSecurityLevel(SecurityLevel.HIGH);
                    vv.repaint();
                }
            });
        }

        @Override
        public void setView(VisualizationViewer vv) {
            this.vv = vv;
            vv.repaint();
        }

        @Override
        public void setNode(Node n) {
            this.n = n;
        }
    }

    /**
     * Implements an entry on the menu containing a node label (with high
     * security level) that after clicked sets it to low security level.
     */
    public class LowLevNodeRadioButton extends JRadioButtonMenuItem
            implements NodeMenuListener<Node> {

        Node n;
        VisualizationViewer vv;

        /**
         * Default constructor.
         */
        public LowLevNodeRadioButton() {
            super("Low Security Level");
            this.addActionListener(new ActionListener() {

                /**
                 * Changes the security level to low.
                 */
                @Override
                public void actionPerformed(ActionEvent e) {
                    n.setSecurityLevel(SecurityLevel.LOW);
                    vv.repaint();
                }
            });
        }

        @Override
        public void setView(VisualizationViewer vv) {
            this.vv = vv;
            vv.repaint();
        }

        @Override
        public void setNode(Node n) {
            this.n = n;
        }
    }
}
